<?xml version="1.0"?>

<st-source>
<time-stamp>From VisualWorks® NonCommercial, 7.6 of March 3, 2008 on March 12, 2009 at 5:47:22 pm</time-stamp>


<class>
<name>CCS</name>
<environment>SupplyChain</environment>
<super>Core.Object</super>
<private>false</private>
<indexed-type>none</indexed-type>
<inst-vars>suppliers retailers </inst-vars>
<class-inst-vars></class-inst-vars>
<imports></imports>
<category></category>
<attributes>
<package>SupplyChain</package>
</attributes>
</class>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<methods>
<class-id>SupplyChain.CCS class</class-id> <category>instance creation</category>

<body package="SupplyChain" selector="new">new
	"Answer a newly created and initialized instance."

	^super new initialize</body>
</methods>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<comment>
<class-id>SupplyChain.CCS</class-id>
<body>The central coordination system (CCS) handles the interface between all the POS locations and all the suppliers in the system.

Instance Variables:
	retailers	&lt;OrderedCollection&gt;	A list of Retailer objects
	suppliers	&lt;OrderedCollection&gt;	A list of supplier objects

</body>
</comment>

<methods>
<class-id>SupplyChain.CCS</class-id> <category>accessing</category>

<body package="SupplyChain" selector="retailers">retailers
	^retailers</body>

<body package="SupplyChain" selector="retailers:">retailers: anObject
	retailers := anObject</body>

<body package="SupplyChain" selector="suppliers">suppliers
	^suppliers</body>

<body package="SupplyChain" selector="suppliers:">suppliers: anObject
	suppliers := anObject</body>
</methods>

<methods>
<class-id>SupplyChain.CCS</class-id> <category>initialize-release</category>

<body package="SupplyChain" selector="initialize">initialize
	"Initialize a newly created instance. This method must answer the receiver."

	" *** Edit the following to properly initialize instance variables ***"
	suppliers := OrderedCollection new.
	retailers := OrderedCollection new.
	" *** And replace this comment with additional initialization code *** "
	^self</body>
</methods>

<methods>
<class-id>SupplyChain.CCS</class-id> <category>load-save</category>

<body package="SupplyChain" selector="load">load
	"Load a previous state"
	|boss filename array ccs|
	filename := Dialog requestFileName: 'Open CCS'.
	(filename = '') ifFalse:[
		boss := BinaryObjectStorage onOldNoScan: filename asFilename readStream.
		[array := boss contents] ensure: [boss close].
		ccs := array at: 1.
		self updateWith: ccs.
	].
	^self.</body>

<body package="SupplyChain" selector="save">save
	"Save the current CCS"
	|boss filename ccs|
	ccs := self deepCopy.
	filename := Dialog requestFileName: 'Open CCS'.
	(filename = '') ifFalse:[
		boss := BinaryObjectStorage onNew: filename asFilename writeStream.
		[boss nextPut: ccs ] ensure: [boss close].
	].
	^self.</body>
</methods>

<methods>
<class-id>SupplyChain.CCS</class-id> <category>updating</category>

<body package="SupplyChain" selector="addRetailer:">addRetailer: aRetailer
	"Add a retaiier"
	aRetailer ccs: self.
	retailers add: aRetailer.
	^self.</body>

<body package="SupplyChain" selector="addSupplier:">addSupplier: aSupplier
	"Add a supplier"
	suppliers add: aSupplier.
	^self.</body>

<body package="SupplyChain" selector="deepCopy">deepCopy
	"Returns a deep copy of the current CCS object instance"
	|ccs|
	ccs := CCS new.
	self retailers do: [ :r | ccs addRetailer: r.].
	self suppliers do: [ :s | ccs addSupplier: s.].
	^ccs.</body>

<body package="SupplyChain" selector="removeRetailer:">removeRetailer: aRetailer
	"Remove a retailer"
	(retailers includes: aRetailer) ifTrue:[
		aRetailer ccs: nil.
		retailers remove: aRetailer.].
	^self.</body>

<body package="SupplyChain" selector="removeSupplier:">removeSupplier: aSupplier
	"Remove a supplier"
	(suppliers includes: aSupplier) ifTrue:[ suppliers remove: aSupplier.].
	^self.</body>

<body package="SupplyChain" selector="updateWith:">updateWith: aCCS
	"Used with BOSS loading to update a newly loaded class instance
	This method is to be used by the AppModel, not the GUI itself. Ignore
	it for now"
	retailers size timesRepeat: [ retailers removeFirst ].
	suppliers size timesRepeat: [ suppliers removeFirst ].
	aCCS retailers do: [ :r | self addRetailer: r.].
	aCCS suppliers do: [ :s | self addSupplier: s.].
	^self.</body>
</methods>

<methods>
<class-id>SupplyChain.CCS</class-id> <category>coordinating</category>

<body package="SupplyChain" selector="handleOrder:">handleOrder: anOrder
	"Handles a single order from a Retailer"
	|d invoices|
	d := Dictionary new.
	invoices := OrderedCollection new.
	(anOrder items) do: [ :item | | supplier |
     		supplier := suppliers detect: [ :s | s sellItem: item].
     		(d includesKey: supplier) ifFalse: [ d add: (Association key: supplier value: (OrderedCollection new)).].
    		(d at: supplier) add: item.].

	d associations do: [:a | invoices add: ((a key) orderItems: (a value)).]. 

	(anOrder retailer) shipmentArrived: invoices.
	^self.

	"(anOrder items) do:[:i|
		suppliers do:[:j|
			(j sellsItem: i) ifTrue:[
				(neededSuppliers includes: j) 
			ifFalse:[
				neededSuppliers add: j.].].].].

	neededSuppliers do:[:i| 
		(anOrder items) do:[:j| (i sellsItem: j) ifTrue:[itemsToInvoice add: j.].].
		invoices add: (i orderItems: itemsToInvoice).
		itemsToInvoice := OrderedCollection new.]."</body>
</methods>

</st-source>
